package login

import (
	"context"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"

	v1 "web-ppt/api/login/v1"
	"web-ppt/internal/consts"
)

func (c *ControllerV1) Login(ctx context.Context, req *v1.LoginReq) (res *v1.LoginRes, err error) {
	res = &v1.LoginRes{
		Url: c.session.Get(ctx, consts.SessionReferer).String(),
	}

	// 验证验证码
	if c.session.Get(ctx, consts.SessionLoginYzm).String() != req.Captcha {
		c.session.Remove(ctx, consts.SessionLoginYzm)
		return nil, gerror.NewCode(gcode.CodeBusinessValidationFailed, "请输入正确的验证码")
	}

	c.session.Set(ctx, consts.User, "editor")
	c.session.Remove(ctx, consts.SessionLoginYzm)

	c.session.Remove(ctx, consts.SessionReferer)
	return res, nil
}
